$(function(){
	
	autoMax();
	
	//初始化微信分享
	initWeixin();
	
	//更新页面布局
	updatePage();
});


/**
 * @Title: updatePage
 * @Description: 更新页面布局
 * @author fengshuonan
 */
function updatePage()
{
	var couponType = $("#couponType").val();
	var isActive = $("#isActive").val();
	
	//如果是出行券
	if(couponType == '2'){
		
		if(isActive == 'true'){
			$(".top-header").css("background","url(img/chuxing-jihuo.png)");
			$(".top-header").css("background-size",'100% 100%');
		}
	}else{
		
		//如果是洗车券
		if(isActive == 'true'){
			$(".banner-img").attr("src","img/xiche-jihuo.png");
		}
	}
}


/**
 * @Title: downLoadApp
 * @Description: 点击下载叮叮搭车app
 * @author fengshuonan
 */
function downLoadApp()
{
	window.location.href = "http://www.pdhz-dddc.cn:8085/appdown";
}

//自动适配屏幕
function autoMax()
{
	var width = document.body.clientWidth;
	var beishu = width/375;  ////375 iphone6屏幕上的标准
	
	$("html").css('font-size',$("html").css('font-size')*beishu);
	var fz = 10*beishu;
	$("html").css('font-size',fz);
}


//分享成功后激活优惠券
function activeCoupon(){
	
	var tellphone = $("#tellphone").val();
	var couponType = $("#couponType").val();
	
	var advId = $("#advId").val();
	
	$.ajax({
		async: false,
        type: "post",
        url: "userCouponAction!activateCoupon.action",
        data: {
        	'tellphone':tellphone,
        	'couponType':couponType
        },
        dataType: "text",
        success: function(data){
        	
        	if(data == 'success'){
        		alert("您已分享成功，优惠券已经激活！");
	        	
        		//刷新页面
	    		window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb36d3ec4a768b571&redirect_uri=http%3a%2f%2fwww.pdhz-dddc.cn%2fdingding-web%2fuserCouponAction!startReceiveCoupon.action%3fcouponType%3d" + couponType + "%26couponMoney%3d" + 2 + "%26advId%3d" + advId + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        	}
        }
    });
}


/**
 * @Title: initWeixin
 * @Description: 初始化微信
 * @param title 分享到朋友圈的标题
 * @param des 分享给朋友的详情
 * @author fengshuonan
 */
function initWeixin(){
	
	var timestampVal = $("#timestamp").val();
	var nonceStrVal = $("#nonceStr").val();
	var signatureVal = $("#signature").val();
	
	var couponType = $("#couponType").val();
	
	var advId = $("#advId").val();
	
	//初始化优惠券的标题和详情
	var title,des;
	
	//如果是出行券
	if(couponType == '2'){
		title = '你的叮叮搭车出行红包，记得使用哦！';
		des = '红包金额随机分配，可直接支付搭车费用哦！更多优惠请关注官方微信：庞大叮叮(dingding-dc)。';
	}else{
		title = '亲，请收下这张免费洗车券！';
		des = '凭此券可到指定店免费洗车一次。更多优惠请关注官方微信：庞大叮叮(dingding-dc)。';
	}
	
	wx.config({
	    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
	    appId: 'wxb36d3ec4a768b571', // 必填，公众号的唯一标识
	    timestamp: timestampVal, // 必填，生成签名的时间戳
	    nonceStr: nonceStrVal, // 必填，生成签名的随机串
	    signature: signatureVal,// 必填，签名，见附录1
	    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage']
	});
	
	wx.ready(function(){
		wx.onMenuShareTimeline({
		    title: title,
		    link: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb36d3ec4a768b571&redirect_uri=http%3a%2f%2fwww.pdhz-dddc.cn%2fdingding-web%2fuserCouponAction!startReceiveCoupon.action%3fcouponType%3d" + $("#couponType").val() + "%26couponMoney%3d" + $("#couponMoney").val() + "%26advId%3d" + advId + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect", // 分享链接
		    imgUrl: "http://ddride-server-image.oss-cn-shanghai.aliyuncs.com/share.png", // 分享图标
		    success: function () { 
		    	
		    	//增加一次推广商的分享次数
		    	addShareNum();
		    	
		    	//激活优惠券
		    	activeCoupon();
		    },
		    cancel: function () { 
		    }
		});
		
		wx.onMenuShareAppMessage({
		    title: title,
		    desc: des,
		    link: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb36d3ec4a768b571&redirect_uri=http%3a%2f%2fwww.pdhz-dddc.cn%2fdingding-web%2fuserCouponAction!startReceiveCoupon.action%3fcouponType%3d" + $("#couponType").val() + "%26couponMoney%3d" + $("#couponMoney").val() + "%26advId%3d" + advId + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect", // 分享链接
		    imgUrl: "http://ddride-server-image.oss-cn-shanghai.aliyuncs.com/share.png", // 分享图标
		    success: function () { 
		    	//增加一次推广商的分享次数
		    	addShareNum();
		    },
		    cancel: function () { 
		    }
		});
		
	});
}


/**
 * @Title: addShareNum
 * @Description: 增加一次推广商的分享次数
 * @author fengshuonan
 */
function addShareNum(){
	
	var advId = $("#advId").val();

	$.ajax({
		async: false,
        type: "post",
        url: "userCouponAction!addShareNum.action",
        data: {
        	'advId':advId
        },
        dataType: "text",
        success: function(data){
        	
        }
    });
}